package com.estronger.shopmanage.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.bean.GridItemBean;

import java.util.List;

/**
 * Created by DC-DingYg on 2016-12-21.
 */

public class MainGridAdapter extends BaseAdapter {
    private Context mContext;

    private List<GridItemBean> giBeans;

    public MainGridAdapter(Context mContext, List<GridItemBean> giBeans) {
        super();
        this.mContext = mContext;
        this.giBeans = giBeans;
    }

    public void notifyDataSetChanged(List<GridItemBean> giBeans){
        this.giBeans = giBeans;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (giBeans == null) {
            return 0;
        }
        return giBeans.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GridItemBean itemBean = giBeans.get(position);
        ViewHolder vh = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.view_gridview_row, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        try {
            vh.tvfText.setText(itemBean.getCtext());
            if(itemBean.getNumber()<=0){
                vh.imgfItem.setImageResource(itemBean.getImg_id());
                vh.tvfNumber.setVisibility(View.INVISIBLE);
            }else {
                vh.imgfItem.setImageResource(itemBean.getImg_id2());
                vh.tvfNumber.setVisibility(View.VISIBLE);
                if(itemBean.getNumber()>99) vh.tvfNumber.setText("99+");
                else vh.tvfNumber.setText(itemBean.getNumber()+"");
            }
        } catch (Exception e) {
        }
        return convertView;
    }
    class ViewHolder {
        TextView tvfText;
        ImageView imgfItem;
        TextView tvfNumber;

        public ViewHolder(View view) {
            this.tvfText = (TextView) view.findViewById(R.id.tvfItemText);
            this.imgfItem = (ImageView) view.findViewById(R.id.imgfItem);
            this.tvfNumber = (TextView) view.findViewById(R.id.tvfItemNum);
        }
    }
}
